<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Models\Attendance\Appeal;
use Illuminate\Support\Facades\Auth;

/**
 * 申述逻辑控制器
 *
 * Date: 2018/6/26
 * @author George
 * @package App\Http\Controllers
 */
class AppealController extends Controller
{
	/**
	 * Date: 2018/6/26
	 * @author George
	 * @param Request $request
	 */
    public function index(Request $request)
    {
    	$this->validate($request, [
    		'project_id' => 'required',
    		'user_id' => 'required',
		]);
    }

	/**
	 * 创建申述记录
	 *
	 * Date: 2018/6/26
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function store(Request $request)
    {
        $attributes = $this->validate($request, [
        	'attendance_id' => 'required',
        	'schedule_id' => 'required',
        	'content' => 'required',
			'type' => ['required', Rule::in(['checkin', 'checkout'])]
		]);

        $user = Auth::user();

        $attributes['company_id'] = $user->company_id;
        $attributes['project_id'] = $user->project_id;
        $attributes['user_id'] = $user->id;

        $appeal = Appeal::create($attributes);

        return stored($appeal);
    }

	/**
	 * Date: 2018/6/26
	 * @author George
	 * @param Appeal $appeal
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function show(Appeal $appeal)
    {
    	return success($appeal);
    }

	/**
	 * 更新状态
	 *
	 * Date: 2018/6/26
	 * @author George
	 * @param Request $request
	 * @param Appeal $appeal
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function update(Request $request, Appeal $appeal)
    {
    	$attributes = $this->validate($request, [
    		'status' => [
    			'required',
				Rule::in(['accepted', 'rejected'])
			]
		]);
    	$appeal->update($attributes);
    	return updated($appeal);
    }

	/**
	 * 删除申述信息
	 *
	 * Date: 2018/6/26
	 * @author George
	 * @param Appeal $appeal
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Exception
	 */
    public function destroy(Appeal $appeal)
    {
    	$appeal->delete();
    	return deleted();
    }
}
